package Function;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Vector;

import Function.Utils.FCFileCopyToMaterial;
import VWorldGameStudio.FCCharacter;
import utils.FCDataSingles;

public class FCCharacterLibrary {
	private Vector<FCCharacter> characterList = new Vector<FCCharacter>();
	public Vector<String> characterName = new Vector<String>();
	private FCFileCopyToMaterial fileCopy = new FCFileCopyToMaterial();
	private static final String CHARACTER_LIST_PATH = "./data/characterlist.vw";
	private static final String CHARACTERS_PATH = "./data/character";
	private int characterallnum=0;//角色总数
	/** 
	 * 初始化人物数据 
	 */
	public FCCharacterLibrary() {
		try {
			File[] f=new File(CHARACTERS_PATH).listFiles();
			this.characterallnum=f.length;
			for(int i=0;i<characterallnum;i++) {
				FCCharacter character = FCDataSingles.getReader().readCharacterData(characterName.size(),f[i].getPath());
				characterList.add(character);
				characterName.add(character.getCharacterName());
				if(character.getCharacterType().equals("npc")) {
					System.out.println("人物名称：" +character.getCharacterName()+" 人物对话："+character.getCharacterEventID()+"" +
							"  人物行走图片："+character.getCharacterPngurl()+" 行动："+character.getCharacterActionID());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 注册人物
	public void registerCharater(boolean isnew, int ID, String name, String type, String actionID, String eventID,
			String pngurl) {
		FCCharacter qw = new FCCharacter();
		if (isnew) {
			pngurl = fileCopy.copyPngToMaterialPng(1, pngurl);
			qw.registerCharater(ID, name, type, actionID, eventID, pngurl);
		} else {
			qw.registerCharater(ID, name, type, actionID, eventID, pngurl);
			int index = ID;
			if(characterName.contains(name)) {
				index = characterName.indexOf(name);
				characterList.remove(index);
				characterName.remove(index);
			}
		}
		characterList.add(qw);
		characterName.add(name);
		FCDataSingles.getWriter().writeCharacterData(qw);
	}
	/**
	 * 获取人物角色实际数量
	 */
	public int getCharacterAllNum(){
		return this.characterallnum;
	}
	/**
	 * 获取当前已加载的角色数量
	 * @return
	 */
	public int getCharacterNum() {
		return characterName.size();
	}


	// ��ȡ����id
	public int getCharacterNewID() {
		return characterName.size();
	}

	// 检测是否曾在该人物
	public boolean isCharacterReprat(String name) {
		for (int i = 0; i < characterName.size(); i++)
			if (characterName.get(i).equals(name))
				return true;
		return false;
	}

	// 根据名称获取人物
	public FCCharacter getCharacterOfName(String name) {
		return characterList.get(characterName.indexOf(name));
	}

}
